
<div class="row animate fadeIn">
    <form id="editMod" class="form-horizontal"  method="post">
        <?php foreach ($result as $moduleData) {?>
        <input type="hidden" id="mod_id" name="moduleID" value="<?php echo $moduleData->module_id; ?>">
        <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Edit Module</i></h5>
            <div class="ibox-tools">
                <button  id="sads" class="btn btn-sm btn-primary save-btn-tab">Save</button>
                <a id = "cancel_edit" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
            </div>
        </div>
        <div class="ibox-content">
            <div class="form-group"><label class="col-lg-3 control-label">Code</label>
                <div class="col-lg-10 input-s">
                    <input type="text" name = "moduleCode" class="form-control" value="<?php echo $moduleData->module_code; ?>" readonly>
                </div>
            </div>
            <div class="form-group"><label class="col-lg-3 control-label">Name</label>
                <div class="col-lg-11 input-s">
                    <input type="text" id="mod_name" value="<?php echo $moduleData->module_name; ?>" name="moduleName" class="form-control">
                </div>
            </div>
            <div class="form-group"><label class="col-lg-3 control-label">Directory Path</label>
                <div class="col-lg-11 input-s">
                    <?php $dir = explode("application/views/", $moduleData->dir_path);?>
                    <input type="hidden" name="pastDir" id="past_mn" value="<?php echo $dir[1]; ?>">
                    <input type="text" value="<?php echo $dir[1]; ?>" name="moduleDir" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Enabled</label>
                <div class="col-lg-10 input-s">
                    <input type="checkbox" name="enableBox" value="ok" class="i-checks" <?php echo ($moduleData->enable_flag == 'Y' ? 'checked' : ''); ?> >
                </div>
            </div>
            <?php }
?>
    </form>
            <label class="col-lg-3 control-label"></label>
           <table class="table table-bordered col-lg-10" style="width:25%;" >
                    <thead>
                        <tr>
                            <th>Menu Name</th>
                            <th style="width:50px;">Action</th>
                        </tr>
                    </thead>
                    <tbody id="resultfound">

                        <?php foreach ($module_data as $forms) {?>
                        <tr>
                            <td id="<?php echo $forms->menu_id; ?>" class="menus"><?php echo $forms->menu_name; ?></td>
                            <input id="menuID" type="hidden" value="<?php echo $forms->menu_id; ?>">
                            <input id="menuName" type="hidden" value="<?php echo $forms->menu_name; ?>">
                            <td><button type="button" class="btn btn-outline btn-info edit" data-toggle="modal" data-target="#myModal">Edit</button></td>
                            </tr>
                        <?php }
?>

                    </tbody>
        </table>
                <div id="addpGroup" class="ibox-content">
                    <h3>Add Module Menu</h3>
                    <form  id="e_module-menu-form" class="form-inline">
                        <input type="hidden" id="e_module-id" name="module-id">
                        <div class="form-group">
                          <div class="col-lg-10 input-s">
                            <input type="text" id="ipgName" name="menu_name" class="form-control">
                          </div>
                          <div class="col-lg-10 input-s">
                            <button class="btn btn-white" id="gana"><i class="fa fa-plus success"></i></button>
                          </div>
                        </div>
                    </form>
                </div>
        </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
        <div class="modal-header">
           <h4 class="modal-title" id="myModalLabel">Edit Menu Name</h4>
        </div>
      <div class="modal-body">
            <div class="form-group">
              <form id="menu_edit_form" method="post">
              <input type="hidden" id="edit_id" name="menu_id_edit">
              <label class="">Menu Name</label><br />
              <input type="hidden" id="p_mn" name="p_mns">
              <input type="hidden" id="p_edit_name" name="p_menu_name_edit">
              <input type="text" id="edit_name" name="menu_name_edit">
            </div>
      </div>
      <div class="modal-footer">
           <button id="edit_menuForm" type="button" class="btn btn-primary" >Save changes</button>
      </div>
      </form>
    </div>
  </div>
</div> <!-- end-of-modal -->

<script>
        $(document).ready(function () {
                var mCode =$('input[name=moduleCode]').val();
                var mName =$('#mod_name').val();
                var mDir  =$('input[name=moduleDir]').val();
                var col ;
                var pmn = $('#past_mn').val();
                $('#e_module-id').val($('#mod_id').val());
                $(document).on('click','.btn.btn-outline.btn-info.edit',function(){
                   col = $(this).closest('td');
                    var menuID          = $(this).closest('tr').children('#menuID').val();
                    var menuName        = $(this).closest('tr').children('.menus').text();
                    $('#edit_id').val(menuID);
                    $('#p_edit_name').val(menuName);
                    $('#edit_name').val(menuName);
                    $('#p_mn').val(pmn);
                });

                $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green',
                });


        $('#edit_menuForm').click(function(){
            if($("#menu_edit_form").valid()==true){
               var mid = $('#edit_id').val();
               var mname = $('#edit_name').val();
            dataString = $("#menu_edit_form").serialize();
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/edit_menu'); ?>",
                        data: dataString,
                        dataType: 'json',
                    success: function(data){
                            if(data.err == 'Y'){
                               var sd = '#'+mid;
                              $('#resultfound tr').find(sd).html(mname);
                              notifymsg("success",". . .Menu Updated");
                              $('#myModal').modal('hide');
                                console.log(col);
                                }
                            }
                        });
                }
        });
           $("#menu_edit_form").validate({
                 rules: {
                     menu_name_edit: {
                         required: true,
                         lettersonly: true
                     }
                 }
            });
        $("#editMod").validate({
                 rules: {
                     moduleCode: {
                         required: true,
                         minlength: 2,
                         capitalonly: true
                     },
                     moduleName: {
                         required: true,
                         lettersonly: true
                     },
                     moduleDir: {
                         required: true,
                         lettersonly: true
                     }
                 },
                 submitHandler: function(form){
                    dataString = $("#editMod").serialize();
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/update_module'); ?>",
                        data: dataString,
                        dataType: 'json',
                    success: function(data){
                           close_edit(); active_search();
                            notifymsg("success"," . . .Update Succesfull");
                            }
                        });
                 }
             });


               $('#cancel_edit').click(function () {
                var mCode_click =$('input[name=moduleCode]').val();
                var mName_click =$('#mod_name').val();
                var mDir_click  =$('input[name=moduleDir]').val();

                if(mCode != mCode_click || mName != mName_click || mDir != mDir_click){
                              swal({
                                          title: "Are you sure?",
                                          text: "Your work will not be save",
                                          type: "warning",
                                          showCancelButton: true,
                                          confirmButtonColor: "#DD6B55",
                                          confirmButtonText: "Leave",
                                          cancelButtonText: "Stay",
                                          closeOnConfirm: true,
                                          closeOnCancel: true },
                                      function (isConfirm) {
                                          if (isConfirm) {
                                            swal("success");
                                            close_edit(); active_search();

                                          } else {
                                              swal("Cancelled");
                                          }
                                      });
                }else{
                    close_edit(); active_search();
                }
                          });

                $("#e_module-menu-form").validate({
                rules:{
                  menu_name: {
                      required: true,
                      lettersonly: true
                  }
                },
                submitHandler: function(form){
                    console.log('clicked!');
                    var trHTML = '';
                $('#tbodydaw').empty();
                var modulename = $('#ipgName').val();
                var moduleid   = $('#mod_id').val();
                console.log(modulename+ ' ' + moduleid);
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/insert_module_menu'); ?>",
                        data: { "menu-name": modulename, "module-id": moduleid  },
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                            notifymsg("success",". . .Menu Added Successfully");
                            $("#change3").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>"+moduleid)
                            edit_action('show'); search_action('close');
                        }
                      });
                 }
               });


                $('#editX').click(function(){
                    close_edit();
                });
            });
</script>

